<template>
  <div class="chat-container">
    <div class="chat-messages" ref="chatMessagesRef">
      <div v-for="msg in messages" :key="msg.id" :class="['chat-message', msg.user === currentUser ? 'me' : 'other']">
        <div class="chat-row">
          <div v-if="msg.user === currentUser" class="chat-avatar">
            <img src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" alt="user1" />
          </div>
          <div v-else class="chat-avatar chat-avatar-text">user</div>
          <div class="chat-main">
            <div class="chat-user">{{ msg.user === currentUser ? '我' : '对方' }}</div>
            <div class="chat-content">{{ msg.text }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="chat-input">
      <input v-model="input" @keydown.enter="sendMessage" placeholder="请输入消息..." />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue';
import { useMessagesStore } from '@/stores/messages'
import { storeToRefs } from 'pinia';

const useMessages = useMessagesStore();
const { messages } = storeToRefs(useMessages);

const currentUser = useMessages.currentUser;
const otherUser = useMessages.otherUser;

const input = ref('');
const chatMessagesRef = ref<HTMLElement | null>(null);

function scrollToBottom() {
  nextTick(() => {
    if (chatMessagesRef.value) {
      chatMessagesRef.value.scrollTop = chatMessagesRef.value.scrollHeight;
    }
  });
}

function sendMessage() {
  if (!input.value.trim()) return;
  messages.value.push({
    id: Date.now(),
    user: currentUser,
    text: input.value,
  });
  input.value = '';
  scrollToBottom();
  // 模拟对方回复
  setTimeout(() => {
    messages.value.push({
      id: Date.now() + 1,
      user: otherUser,
      text: '收到：' + messages.value[messages.value.length - 1].text,
    });
    scrollToBottom();
  }, 800);
}
</script>

<style scoped>
.chat-container {
  max-width: 400px;
  width: 400px;
  height: 100%;
  margin: 0;
  box-sizing: border-box;
  background: #ededed;
  border-radius: 0 8px 8px 0;
  padding: 10px;
  display: flex;
  flex-direction: column;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 16px;
  scrollbar-width: none;
  /* Firefox 隐藏滚动条 */
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
  background: transparent;
  opacity: 0;
  transition: opacity 0.2s;
}

.chat-messages:hover::-webkit-scrollbar {
  opacity: 1;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #b3b3b3;
  border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-track {
  background: #f2f3f5;
  border-radius: 4px;
}

.chat-messages::-webkit-scrollbar {
  width: 0px;
  background: transparent;
  transition: width 0.2s;
}

.chat-messages:hover::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #b3b3b3;
  border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-track {
  background: #f2f3f5;
  border-radius: 4px;
}

.chat-message {
  margin-bottom: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.chat-message {
  margin-bottom: 12px;
}

.chat-row {
  display: flex;
  align-items: flex-end;
}

.chat-message.me .chat-row {
  flex-direction: row-reverse;
}

.chat-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 8px;
  overflow: hidden;
}

.chat-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-avatar-text {
  font-size: 16px;
  color: #fff;
  background: #409eff;
  font-weight: bold;
}

.chat-main {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.chat-message.me .chat-main {
  align-items: flex-end;
  text-align: right;
}

.chat-message.me {
  align-items: flex-end;
}

.chat-message.me .chat-content {
  margin-left: auto;
}

.chat-content {
  background: #f2f3f5;
  border-radius: 6px;
  padding: 8px 10px;
  max-width: 90%;
  word-break: break-all;
}

.chat-message.me .chat-content {
  background: #c6e2ff;
}

.chat-input {
  display: flex;
  gap: 8px;
}

input {
  flex: 1;
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #ddd;
}

button {
  padding: 8px 16px;
  border-radius: 4px;
  border: none;
  background: #409eff;
  color: #fff;
  cursor: pointer;
}

button:hover {
  background: #66b1ff;
}
</style>
